package controller;

import org.apache.log4j.Logger;

import br.com.tcc.dao.AnuncianteDAO;
import br.com.tcc.model.Anunciante;
import br.com.tcc.model.Endereco;
import crawler.MyCrawlerAmericanas;
import edu.uci.ics.crawler4j.crawler.CrawlConfig;
import edu.uci.ics.crawler4j.crawler.CrawlController;
import edu.uci.ics.crawler4j.fetcher.PageFetcher;
import edu.uci.ics.crawler4j.robotstxt.RobotstxtConfig;
import edu.uci.ics.crawler4j.robotstxt.RobotstxtServer;

public class ControllerAmericanas {
	public static void main(String[] args) throws Exception {
		
		Logger logger = Logger.getLogger(Controller.class);
//		adicionarAnunciante();
		
		logger.info("[INICIO] main(String[] args)");
		
		String crawlStorageFolder = "D:\\temp";
        int numberOfCrawlers = 10;
        
        CrawlConfig config = new CrawlConfig();
        config.setCrawlStorageFolder(crawlStorageFolder);
//        config.setProxyHost("155.1.5.17");
//        config.setProxyPort(3128);
//        config.setProxyUsername("fernando");
//        config.setProxyPassword("fernando111");
        /*
         * Instantiate the controller for this crawl.
         */
       
        PageFetcher pageFetcher = new PageFetcher(config);
        RobotstxtConfig robotstxtConfig = new RobotstxtConfig();
        RobotstxtServer robotstxtServer = new RobotstxtServer(robotstxtConfig, pageFetcher);
        CrawlController controller = new CrawlController(config, pageFetcher, robotstxtServer);
        /*
         * For each crawl, you need to add some seed urls. These are the first
         * URLs that are fetched and then the crawler starts following links
         * which are found in these pages
         */
        
        controller.addSeed("http://www.americanas.com.br");
       

        /*
         * Start the crawl. This is a blocking operation, meaning that your code
         * will reach the line after this only when crawling is finished.
         */
        controller.start(MyCrawlerAmericanas.class, numberOfCrawlers);    
    }

	private static void adicionarAnunciante() {
		 AnuncianteDAO aDAO = new AnuncianteDAO();
		 
		 Endereco endereco = new Endereco(2, "","","","","");
		 Anunciante anunciante = new Anunciante(2, endereco, "Americanas", "americanas@gmail.com","teste2", " ", " ", " ", "Americanas.com"," ", "22222222222222", "A");
		 
         aDAO.adicionar(anunciante);
	}
}